<?php
namespace app\admin\model;

use think\Model;
use think\Db;

class BusSxcallback extends Model
{
	public function ListData($whereData)
	{
		$data = $this->alias('cb')->field('cb.*,cha.company_name')->join('bus_channel cha','cb.channel_id=cha.channel_id','LEFT');
		$where = [];

		foreach ($whereData as $key => $value)
		{
			if($value != '')
			{
				$value = trim($value);
				$where['cb.'.$key] = $value;
			}
		}


		$where['cb.moTime'] = ['between',[$whereData['st'],$whereData['et']]];
		unset($where['cb.st']);
		unset($where['cb.et']);


		switch (session('user_type')) {
			case 'admin':

				break;
			case 'channel':
				if(!session('relate_id'))
				{
					exit('未绑定渠道');
				}
				$channel = $this->getChannel();
				$channelarr = [];
				foreach ($channel as $key => $value) {
					$channelarr[] = $value['channel_id'];
				}
				$data = $data->where('cb.channel_id','in',$channelarr);
				break;
			default:
				return [];
				break;
		}

		if(isset($where['cb.SpNumber']))
		{
			$data = $data->where("0+cb.SpNumber='".$where['cb.SpNumber']."'");
			unset($where['cb.SpNumber']);
		}

		$data = $data->where($where)->order('cb.moTime desc')->paginate(15,false,['query' => request()->param()])
	    	->each(function($item, $key){
	    		$item->moMsg = htmlspecialchars($item->moMsg, ENT_NOQUOTES);
	  //   		// switch ($item->status) {
	  //   		// 	case '1':
	  //   		// 		$item->status_ = "<span class='label label-success radius'>已通过</span>";
	  //   		// 		break;
	  //   		// 	case '2':
	  //   		// 		$item->status_ = "<span class='label label-warning radius'>待审核</span>";
	  //   		// 		break;
	  //   		// 	case '3':
	  //   		// 		$item->status_ = "<span class='label label-danger radius'>未通过</span>";
	  //   		// 		break;

	  //   		// 	default:
	  //   		// 		# code...
	  //   		// 		break;
	  //   		// }

			});

		return $data;
	}

	public function getAlls($request)
	{
		$where = [];

		$data = $this->alias('cb')
			->field('cb.*,cha.company_name')
			->join('bus_channel cha','cb.channel_id=cha.channel_id','LEFT')
			->where('cb.moTime','between time',[$request['st'], $request['et']]);
		//时间
		// $where['cb.moTime'] = ['between time', [$request['st'], $request['et']] ];

		if (!empty($request['phone'])) {

			$where['cb.phone'] = $request['phone'];
		}

		if (!empty($request['SpNumber'])) {

			$where['cb.SpNumber'] = $request['SpNumber'];
		}

		switch (session('user_type')) {

			case 'admin':

				break;

			case 'channel':

				if(!session('relate_id')) {

					return ['msg' => '未绑定渠道'];
				}

				$channel = $this->getChannel();
				$channelarr = [];

				foreach ($channel as $key => $value) {

					$channelarr[] = $value['channel_id'];
				}

				$data = $data->where('cb.channel_id', 'in', $channelarr);

				break;

			default:
				return [];
				break;
		}

		if (!empty($request['channel_id'])) {

			$where['cb.channel_id'] = $request['channel_id'];
		}

		if(isset($where['cb.SpNumber'])) {

			$data = $data->where("0+cb.SpNumber='".$where['cb.SpNumber']."'");
			unset($where['cb.SpNumber']);
		}

		$data = $data->where($where)->order('cb.moTime desc')->paginate($request['limit'], false, $request)
		    	->each(function($item, $key){
		    		$item->moMsg = htmlspecialchars($item->moMsg, ENT_NOQUOTES);
				});

		return $data;
	}

	// 导出获取数据
	public function getExport($whereData, $page = 1, $limit = 20000)
	{
		$data = $this->alias('cb')
			->field('cb.channel_id, cha.company_name, cb.phone, cb.SpNumber, cb.moMsg, cb.moTime')
			->join('bus_channel cha','cb.channel_id=cha.channel_id','LEFT');

		$where = [];
		foreach ($whereData as $key => $value)
		{
			if($value != '')
			{
				$value = trim($value);
				$where['cb.'.$key] = $value;
			}
		}

// halt($where);

		// $where['cb.moTime'] = ['between',[$whereData['st'],$whereData['et']]];
		$data = $data->where('cb.moTime','between time',[$whereData['st'],$whereData['et']]);
		unset($where['cb.st']);
		unset($where['cb.et']);


		switch (session('user_type')) {
			case 'admin':

				break;
			case 'channel':
				if(!session('relate_id'))
				{
					exit('未绑定渠道');
				}
				$channel = $this->getChannel();
				$channelarr = [];
				foreach ($channel as $key => $value) {
					$channelarr[] = $value['channel_id'];
				}
				$data = $data->where('cb.channel_id','in',$channelarr);
				break;
			default:
				return [];
				break;
		}

		if(isset($where['cb.SpNumber']))
		{
			$data = $data->where("0+cb.SpNumber='".$where['cb.SpNumber']."'");
			unset($where['cb.SpNumber']);
		}

		return $data->where($where)->order('cb.moTime desc')->page($page . ',' . $limit)->select();
	}

	public function getChannel()
	{
		$channel = [];
		switch (session('user_type')) {
			case 'admin':
				$channel = Db::name('bus_channel')->field('channel_id,company_name')->select();
				break;
			case 'channel':
				$channel = Db::name('bus_channel')->field('channel_id,company_name')->where('p_id',session('relate_id'))->whereOr('channel_id',session('relate_id'))->select();
				break;

			default:
				return [];
				break;
		}


		return $channel;
	}

	public function getCount($whereData)
	{
		$data = $this->alias('cb')
			->field('cb.channel_id, cha.company_name, cb.phone, cb.SpNumber, cb.moMsg, cb.moTime')
			->join('bus_channel cha','cb.channel_id=cha.channel_id','LEFT');
		$where = [];

		foreach ($whereData as $key => $value)
		{
			if($value != '')
			{
				$value = trim($value);
				$where['cb.'.$key] = $value;
			}
		}


		// $where['cb.moTime'] = ['between',[$whereData['st'],$whereData['et']]];

		$data = $data->where('cb.moTime','between time',[$whereData['st'],$whereData['et']]);

		unset($where['cb.st']);
		unset($where['cb.et']);


		switch (session('user_type')) {
			case 'admin':

				break;
			case 'channel':
				if(!session('relate_id'))
				{
					exit('未绑定渠道');
				}
				$channel = $this->getChannel();
				$channelarr = [];
				foreach ($channel as $key => $value) {
					$channelarr[] = $value['channel_id'];
				}
				$data = $data->where('cb.channel_id','in',$channelarr);
				break;
			default:
				return [];
				break;
		}

		if(isset($where['cb.SpNumber']))
		{
			$data = $data->where("0+cb.SpNumber='".$where['cb.SpNumber']."'");
			unset($where['cb.SpNumber']);
		}
// trace($data->where($where)->fetchSql(true)->count());
// halt('1');
// halt($where);
		return $data->where($where)->count();
	}


}